package com.example.ws.api;

import com.example.ws.config.DemoWebsocketHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class DemoController {
    private static final Logger LOGGER = LoggerFactory.getLogger(DemoController.class);
    @Resource
    private DemoWebsocketHandler demoWebsocketHandler;


    @RequestMapping("login")
    @ResponseBody
    public Map<String,Object> api(String account, HttpServletRequest httpServletRequest){
        HashMap<String, Object> hashMap = new HashMap<>();
        if(account.equals("tom") || account.equals("jerry")){
            HttpSession session = httpServletRequest.getSession();
            session.setAttribute("SESSION_USERNAME", account);
            hashMap.put("ok", 1);
        }else{
            hashMap.put("ok", -1);
        }
        return hashMap;
    }


    @RequestMapping("/send")
    @ResponseBody
    public Map<String,Object> send(String account){
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("ok", 1);
        demoWebsocketHandler.send("我是服务器消息",account );
        return result;
    }


    @RequestMapping("msg")
    public String msg(){
        return "message";
    }




}
